package main

import (
	"fmt"
	"gitee.com/Diegoh/go-study/go-logagent/conf"
	"time"

	"gitee.com/Diegoh/go-study/go-color/text"
	"gitee.com/Diegoh/go-study/go-logagent/kafka"
	"gitee.com/Diegoh/go-study/go-logagent/taillog"
)



func main() {
	 conf.Init()
	fmt.Printf("config:%v",conf.Cfg)
	config:=conf.Cfg
	//1.初始化kafka链接
	err := kafka.Init([]string{config.Kafka.Addr})
	if err != nil {
		fmt.Println(text.Red("kafka init fail"), err)
		return
	}
	fmt.Println("init kafka success")
	//2.打开日志准备收集日志
	err = taillog.Init(config.Taillog.Path)
	if err != nil {
		fmt.Println("开日志准备收集日志错误", err)
		return
	}
	fmt.Println("init taillog success")

	run()
}

/**
读取日志发送到kafka
*/
func run() {
	for {
		select {
		case line := <-taillog.ReadChan():
			kafka.SendToKafka(conf.Cfg.Taillog.Path, line.Text)
		default:
			time.Sleep(time.Second)
		}
	}

}
